﻿using Aop.Api;
using Aop.Api.Domain;
using Aop.Api.Request;
using Aop.Api.Response;
using Lg.Core.Extensions;
using Lg.Core.Pay;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Lg.Core.AliHelper
{
    public class AliPay:IPay
    {
        private AliPayConfig? _config;

        public AliPay()
        {
            JsonHelper jsonHelper = new JsonHelper();
            _config = new AliPayConfig()
            {
                app_id = jsonHelper.GetValue("AliPay.app_id"),
                alipay_public_key = jsonHelper.GetValue("AliPay.alipay_public_key"),
                private_key = jsonHelper.GetValue("AliPay.private_key"),
                gatewayUrl = jsonHelper.GetValue("AliPay.gatewayUrl"),
                NotifyUrl = jsonHelper.GetValue("AliPay.NotifyUrl"),
                ReturnUrl = jsonHelper.GetValue("AliPay.ReturnUrl")
            };
            Log.Info("AliPay",_config.ToJsonString());
        }
        public AliPay(AliPayConfig config)
        {
            this._config=config;
        }
        public AliPay(string app_id, string gatewayUrl, string private_key, string alipay_public_key, string sign_type, string charset, string ReturnUrl, string NotifyUrl)
        {
            this._config = new AliPayConfig
            {
                app_id = app_id,
                gatewayUrl = gatewayUrl,
                private_key = private_key,
                alipay_public_key = alipay_public_key,
                sign_type = sign_type,
                charset = charset,
                ReturnUrl = ReturnUrl,
                NotifyUrl = NotifyUrl,
            };  
        }
        public string AppPay(string str)
        {
            throw new NotImplementedException();
        }

        public string MicroPay(string str)
        {
            throw new NotImplementedException();
        }

        public string NativePay(string str)
        {
            throw new NotImplementedException();
        }

        public string PcPay(string str)
        {
            throw new NotImplementedException();
        }

        public IPayResult? WapPay(IPayParam param)
        {
            if (this._config != null)
            {
                AliWapPayParam? model = param as AliWapPayParam;
                if (model != null)
                {
                    model.ProductCode = "QUICK_WAP_WAY";
                    DefaultAopClient? client = new DefaultAopClient(this._config.gatewayUrl, this._config.app_id, this._config.private_key, "json", "1.0", this._config.sign_type, this._config.alipay_public_key, this._config.charset, false);
                    AlipayTradeWapPayRequest? request = new AlipayTradeWapPayRequest();
                    // 设置支付完成同步回调地址
                    request.SetReturnUrl(this._config.ReturnUrl);
                    // 设置支付完成异步通知接收地址
                    request.SetNotifyUrl(this._config.NotifyUrl);
                    // 将业务model载入到request
                    request.SetBizModel(model);
                    var response = client?.pageExecute(request, null, "post");
                    if(response != null && !response.IsError)
                    {
                        return new AliWapPayResult { Body = response?.Body };
                    }
                    else
                    {
                        throw new Exception("支付异常！");
                    }
                    
                }
                else
                {
                    throw new Exception("AliPay支付参数格式不正确！");
                }
                
            }
            else
            {
                throw new Exception("AliPay缺少主要支付参数");
            }
        }
    }
}
